# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: xguo, donggu

import requests
from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo


orderbook_unit_map = {}


def gen_var_ticks():
  price_list = [1e09, 500000., 200000., 50000., 5000., 500., 10., 1., 0]
  tick_list = [1000., 500., 100., 10., 1., 0.1, 0.01, 0.001]

  range_list = list(zip(price_list[:-1], price_list[1:]))
  result = []
  for r, tick in zip(range_list, tick_list):
    assert r[0] > r[1]
    info = {
        'from': r[1],
        'to': r[0],
        'value': tick,
    }
    result.append([info['from'], info['to'], info['value']])
  return result


def generate():
  mea = 'Spot.Coinone.v2'
  me = 'Spot.Coinone'
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  data = requests.get("https://api.coinone.co.kr/public/v2/markets/KRW").json()["markets"]
  orderbook_unit_map.clear()

  pis = []
  for info in data:
    native_base = info['target_currency']
    native_quote = info['quote_currency']
    native_symbol = f'{native_base}-{native_quote}'
    base = symbol_info.native_to_norm_currency(mea, native_base)
    quote = symbol_info.native_to_norm_currency(mea, native_quote)
    symbol = "%s-%s" % (base, quote)

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        is_inverse=False,
        price_ticksize=float(info['price_unit']),
        qty_ticksize=float(info['qty_unit']),
        min_amount=float(info["min_order_amount"])
    )

    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


def gen_coin1_constants():
  data = generate()
  data = convert_coin1_constants(data)
  return data


def convert_coin1_constants(data):
  native_coin_list = list({info['native_base'].lower() for info in data})
  return {
      "native_coin_list": native_coin_list,
      "orderbook_unit_map": orderbook_unit_map,
  }


if __name__ == "__main__":
  import json
  print(json.dumps(generate(), indent=2))
